<template>
  <div style="padding-bottom: 50px">
    <!-- 搜索框 -->
    <van-search
      shape="round"
      background="#c82519"
      placeholder="请输入搜索关键词"
    />
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe
        style="height: 200px; width: 100%"
        :autoplay="3000"
        indicator-color="white"
      >
        <van-swipe-item v-for="item in bannerList" :key="item.bannerid">
          <img style="width: 100%; height: 100%" :src="item.img" />
        </van-swipe-item>
      </van-swipe>
      <!-- 菜单列表 -->
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in menuList"
          :key="item.id"
          :icon="item.icon"
          :text="item.title"
        />
      </van-grid>
      <!-- 商品列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
        class="goods-list"
      >
        <!-- 声明式导航 -->
        <!-- <router-link
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          :to="`/detail?proid=${item.proid}`"
        > -->
        <!-- 编程式导航 -->
        <div
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          @click="$router.push(`/detail?proid=${item.proid}`)"
        >
          <!-- <img :src="item.img1" @error="loadDefaultPicture" /> -->
          <img v-img="item.img1" />
          <span class="title">{{ item.proname }}</span>
          <span class="desc">{{ item.desc }}</span>
          <span class="price">￥{{ item.originprice | formatPrice }}</span>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getBannerList, getMenuList, getGoodsList } from '@/api/home'
export default {
  name: 'Home',
  data () {
    return {
      bannerList: [], // 轮播图列表
      menuList: [], // 菜单分类列表
      count: 0, // 页码
      // limitNum: 10 // 页容量
      goodsList: [],
      loading: false, // 加载状态
      finished: false, // 是否加载完毕
      isLoading: false // 是否正在下拉加载
    }
  },
  created () {
    this.getBannerListData()
    this.getMenuListData()
    // 测试加载第一页数据，后面注释起来，因为我们到时候使用List，它里面会调用getGoodsListData
    // this.getGoodsListData()
  },
  deactivated () {
    console.log('---home---deactivated---')
  },
  activated () {
    console.log('---home---activated---')
  },
  beforeDestroy () {
    console.log('---home---beforeDestroy---')
  },
  destroyed () {
    console.log('---home---destroyed---')
  },
  // mounted () {
  //   window.scroll({ top: 0, left: 0 })
  // },
  methods: {
    async getBannerListData () {
      const res = await getBannerList()
      this.bannerList = res.data
    },
    async getMenuListData () {
      const res = await getMenuList()
      this.menuList = res.data
    },
    /**
     * 该方法第一进来要调用，上拉加载更多和下拉刷新都要调用
     */
    async getGoodsListData () {
      this.count++

      const res = await getGoodsList(this.count)
      this.goodsList = [...this.goodsList, ...res.data]

      if (res.data.length === 0) {
        // 没有数据啦
        this.finished = true
      }

      // 一定要把这个属性设置为false，否则滚动到下面就不会加载了
      this.loading = false
      // 数据加载完毕，把下刷刷新状态，设置为false
      this.isLoading = false
    },
    onRefresh () {
      // 把之前的数据全清空
      this.goodsList = []

      // 重新加载第一页的数据
      this.count = 0
      this.getGoodsListData()
    }
    // loadDefaultPicture (e) {
    //   // console.log(e.target)
    //   e.target.src =
    //     'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
    // }
  }
}
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  box-sizing: border-box;
  width: 100%;

  .goods-item {
    box-sizing: border-box;
    background-color: #fff;
    padding: 5.33333vw;
    border-radius: 1.33333vw;
    display: flex;
    align-items: center;
    width: 48%;
    flex-direction: column;
    margin-top: 1.33333vw;
    img {
      width: 26.66667vw;
      height: 32vw;
    }
    .title {
      font-size: 3.73333vw;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 4vw;
      color: #000;
    }
    .desc {
      font-size: 3.2vw;
      color: #c3c3c3;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-top: 2.66667vw;
    }
    .price {
      font-size: 3.2vw;
      margin-top: 2.66667vw;
      color: red;
    }
  }
}
// 深度作用选择器
::v-deep .van-list__finished-text {
  width: 100%;
  text-align: center;
}
</style>
